package com.tensquare.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.anntation.IgnoreResponseAdvice;
import com.tensquare.controller.BaseController;
import com.tensquare.entity.City;
import com.tensquare.entity.Result;
import com.tensquare.service.ICityService;

/**
 * <p>
 * 城市 前端控制器
 * </p>
 *
 * @author caoxingsheng
 * @since 2019-05-31
 */
@RestController
@RequestMapping("/city")
@CrossOrigin
public class CityController extends BaseController {
	
	@Autowired
	private ICityService cityService;
	
	/**
	 * 查询
	 * @param id
	 * @return
	 */
	@GetMapping("/getById/{id}")
	public Result getById(@PathVariable("id") String id) {
		City city = cityService.getById(id);
		System.out.println(city);
		return setResultSuccess(city);
	}
	/**
	 * 分页查询
	 * @param id
	 * @return
	 */
	@GetMapping("/listPage")
	public Result getById(@RequestBody City city, Page<City> page) {
		Wrapper<City> queryWrapper = new QueryWrapper<City>(city);
		IPage<City> pageResult = cityService.page(page, queryWrapper);
		return setResultSuccess(pageResult);
	}
	
	/**
	 * 添加
	 * @param city
	 * @return
	 */
	@PostMapping("/save")
	public Result save(@RequestBody City city) {
		String id = IdWorker.getIdStr();
		city.setId(id);
		boolean result = cityService.save(city);
		return setResultSuccess(result);
	}
	/**
	 * 更新
	 * @param city
	 * @return
	 */
	@PutMapping("/update")
	public Result update(@RequestBody City city) {
		boolean result = cityService.updateById(city);
		return setResultSuccess(result);
	}
	/**
	 * 删除
	 * @param city
	 * @return
	 */
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable String id) {
		boolean result = cityService.removeById(id);
		return setResultSuccess(result);
	}
	
}
